public class Problem34 {
	public static void main(String[] args) {
		int[] fact = new int[10];
		for (int i = 0; i < fact.length; ++i)
			fact[i] = factorial(i);

		int limit = 362880*7;
		int sum = 0;
		for (int i = 3; i <= limit; ++i) {
			int m = i;
			int s = 0;
			while (m > 0) {
				s += fact[m%10];
				m /= 10;
			}

			if (s == i)
				sum += i;
		}

		System.out.println("" + sum);
	}

	private static int factorial(int x) {
		if (x <= 1)
			return 1;
		return x*factorial(x-1);
	}
}
